package cn.stronglink.oil.user;

import android.os.Handler;
import android.widget.Toast;

import cn.stronglink.oil.MyApplication;
import cn.stronglink.oil.R;

/**
 * Created by Tgb on 2017/10/19.
 * 登录presenter
 */

public class LoginPresenter implements ILoginPresenter {
    private ILoginView loginView;
    private ILoginModel loginModel;
    private Handler mhandler = new Handler();

    public LoginPresenter(ILoginView loginView) {
        this.loginView = loginView;
        this.loginModel = new LoginModel();
    }

    @Override
    public void login() {
        this.loginView.showProgress(MyApplication.getInstance().getString(R.string.login_loading));
        this.loginModel.login(loginView.getUserName(),loginView.getPassword(), new OnLoginListener() {
            @Override
            public void loginSuccess(final User user) {
                mhandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.hideProgerss();
                        loginView.loginSuccess(user);
                    }
                });
            }

            @Override
            public void loginFail(final Exception e) {
                mhandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.hideProgerss();
                        loginView.loginFail(e);
                    }
                });
            }
        });
    }
}
